VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsButton"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'====================================================================================
'clsButton - A menu button (with normal / highlighted styles)
'====================================================================================
Option Explicit

Private Buffer(1) As New clsText  'Cached images of normal/highlighted text
Private MyText As String          'Text for button

Public X As Long                  'X Position on screen in px (for hit-testing)
Public Y As Long                  'Y Position on screen in px (for hit-testing)

'====================================================================================
'CLASS Initialize
'====================================================================================
Private Sub Class_Initialize()
    'Set the two buffers to show normal and highlighted font stlyes by default
    Buffer(0).FontStyle = 1
    Buffer(1).FontStyle = 2
End Sub

'SET Font: Set the default font to use for the text, see 'clsFont'
'====================================================================================
Public Property Set Font(ByRef FontClass As clsFont)
    'Change the base fonts on the buffers (this will cause them to re-render as
    'appropriate)
    Set Buffer(0).DefaultFont = FontClass
    Set Buffer(1).DefaultFont = FontClass
End Property

'LET Text: Set text for the button
'====================================================================================
Public Property Let Text(ByVal NewValue As String)
    'If the value does not differ, then leave
    If NewValue = MyText Then Exit Property
    
    MyText = NewValue
    Buffer(0).Text = NewValue: Buffer(1).Text = NewValue
End Property

'GET Width: Return the width of the buffered text in pixels
'====================================================================================
Public Property Get Width() As Long: Width = Buffer(0).Width: End Property

'GET Height: Return the height of the buffered text in pixels
'====================================================================================
Public Property Get Height() As Long: Height = Buffer(0).Height: End Property

'GET hDC: Return the device context handle of the buffered image
'====================================================================================
Public Property Get hDC(ByVal Highlighted As Boolean) As Long
    hDC = Buffer(Abs(Highlighted)).hDC
End Property

'====================================================================================
'HitTest: Is the mouse over the button?
'====================================================================================
Public Function HitTest(ByVal X As Long, ByVal Y As Long) As Boolean
    'Check if the provided X/Y is within the boundaries of the button
    HitTest = ((X - Me.X) >= 0 And (X - Me.X) <= Buffer(0).Width And _
               (Y - Me.Y) >= 0 And (Y - Me.Y) <= Buffer(0).Height)
    'Change the mousepointer
    SharedAPI.ChangeCursor IIf(HitTest, IDC_LINK, IDC_ARROW)
End Function

'====================================================================================
'CenterX: Center the button horizontally on the screen
'====================================================================================
Public Sub CenterX(ByVal ScreenWidth As Long)
    Me.X = (ScreenWidth - Buffer(0).Width) \ 2
End Sub

'====================================================================================
'Draw: Draw the back buffer onto the provided surface
'====================================================================================
Public Sub Draw(ByVal DesthDC As Long, Optional ByVal Selected As Boolean = False)
    'The X and Y location to draw in are properties to this class. This is done so
    'that this class knows at all times where the text is within the screen, for
    'hit testing (mouseover)
    Buffer(Abs(Selected)).Draw DesthDC, Me.X, Me.Y
End Sub

'====================================================================================
'CLASS Terminate
'====================================================================================
Private Sub Class_Terminate()
    Erase Buffer
End Sub

'=== END ============================================================================
'This work is licenced under the Creative Commons Attribution 2.5 License
